//
//  BusinessViewController.m
//  BProject
//
//  Created by lanou3g on 15/8/14.
//  Copyright (c) 2015年 久远的回忆. All rights reserved.
//

#import "BusinessViewController.h"
#import "PKRequestManager.h"
#import "BusinessCell.h"
#import "BusinessWebViewController.h"
#import "LORefresh.h"

typedef NS_ENUM(NSInteger, RefreshType){
    LORefreshNew,
    LORefreshMore
};

@interface BusinessViewController ()<UICollectionViewDelegateFlowLayout>

@property (nonatomic, strong) NSMutableArray *array;
@property (nonatomic,assign)RefreshType type;
@property (nonatomic,strong) NSString * last_time;

@end

@implementation BusinessViewController

- (NSMutableArray *)array
{
    if (_array == nil) {
        self.array = [[NSMutableArray alloc]initWithCapacity:0];
    }
    return _array;
}

- (void)requestData{
    
    NSString * url = [NSString stringWithFormat:@"http://app.qdaily.com/app/categories/index/18/%@.json",_last_time];

    [PKRequestManager manager:GET requestWith:url parDic:nil finish:^(NSData *data) {
        //成功
        
        if (_type == LORefreshNew) {
            [_array removeAllObjects];
        }
        
        NSDictionary *dataDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        NSArray *listArray = dataDic[@"response"][@"feeds"][@"list"];
        _last_time = [NSString stringWithFormat:@"%@",dataDic[@"response"][@"feeds"][@"last_time"]];
        for (NSDictionary *dic in listArray) {
            BusinessModel  *model = [[BusinessModel alloc] init];
            [model setValuesForKeysWithDictionary:dic];
            [self.array addObject:model];
        }
        
        [self.collectionView.defaultFooter endRefreshing];
        [self.collectionView.defaultHeader endRefreshing];
        [self.collectionView reloadData];

    } error:^(NSError *error) {
        //
       
    }];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    
    _last_time  = @"0";
    self.collectionView.backgroundColor = [UIColor whiteColor];
    [self.collectionView registerClass:[BusinessCell class] forCellWithReuseIdentifier:@"mycell"];
    [self requestData];
    
    __weak BusinessViewController * controller = self;
    [self.collectionView addRefreshWithRefreshViewType:LORefreshViewTypeHeaderDefault refreshingBlock:^{
        _last_time = @"0";
        _type = LORefreshNew;
        [controller requestData];
    }];
    
    [self.collectionView addRefreshWithRefreshViewType:LORefreshViewTypeFooterDefault refreshingBlock:^{
        _type = LORefreshMore;
        [controller requestData];
    }];
    
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return _array.count;
}

//每个item的尺寸;
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    return CGSizeMake(SCREEN_WIDTH, 200);
    
    
    
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    BusinessCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"mycell" forIndexPath:indexPath];
    cell.model = _array[indexPath.item];
    return cell;
    
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    BusinessWebViewController *businessWebVC = [[BusinessWebViewController alloc] init];
    businessWebVC.model = _array[indexPath.item];
    //self.navigationController.navigationBar.hidden = YES;
    [self.navigationController pushViewController:businessWebVC animated:YES];
}




/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

#pragma mark - UIScrollViewdelegate methods
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    for(MJCollectionViewCell *view in self.collectionView.visibleCells) {
        CGFloat yOffset = ((self.collectionView.contentOffset.y - view.frame.origin.y) / IMAGE_HEIGHT) * IMAGE_OFFSET_SPEED;
        view.imageOffset = CGPointMake(0.0f, yOffset);
    }
}

@end
